package com.cn.unarmed.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cn.unarmed.base.BaseActivity;
import com.cn.unarmed.bean.DataHolder;

import java.util.ArrayList;
import java.util.List;

public class DifferentGridItemActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_different_grid_item);

        initView();
    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        if (component.equals(TitleBar.TITLE)) {
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    protected GridView gridView;
    protected List<String> list;

    private void initView() {
        gridView = (GridView) this.findViewById(R.id.grid_rooms);
    }

    private void initData() {
        list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            list.add(String.valueOf(i));
        }
        ThisAdapter adapter = new ThisAdapter(this, list);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            }
        });
    }

    private class ThisAdapter extends BaseAdapter {

        protected List<String> list;
        protected Context context;
        protected LayoutInflater layoutInflater;

        public ThisAdapter(Context context, List<String> list) {
            this.list = list;
            this.context = context;
            layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.item_grid, null);
                holder = new Holder();
                holder.txt_room = (TextView) convertView.findViewById(R.id.txt_room);
                holder.lay_item = (LinearLayout) convertView.findViewById(R.id.lay_item);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.txt_room.setText(list.get(position));
//          屏幕像素宽度
            return convertView;
        }

        class Holder {
            TextView txt_room;
            LinearLayout lay_item;
        }
    }
}
